package com.abel.bigwater.dataserver.model.qdies

import com.abel.bigwater.dataserver.model.PacketDataLenException
import io.netty.buffer.ByteBuf

class DtenAskMeterConfigData(_len: Int) : D10BaseData(_len) {
    override var type = FuncType.AskMeterConfig

    var addr485: Byte? = null

    var addrAnalog: Byte? = null

    var addrPulse: Byte? = null

    var addrExt: Byte? = null

    override fun fill(buf: ByteBuf) {
        assert(addr485 != null && addrAnalog != null && addrPulse != null && addrExt != null)

        buf.apply {
            writeByte(addr485!!.toInt())
            writeByte(addrAnalog!!.toInt())
            writeByte(addrPulse!!.toInt())
            writeByte(addrExt!!.toInt())
        }
    }

    override fun parse(buf: ByteBuf): Boolean {
        if (buf.readableBytes() < MIN_LEN) {
            throw PacketDataLenException("should contain ${MIN_LEN} bytes")
        }

        buf.apply {
            addr485 = readByte()
            addrAnalog = readByte()
            addrPulse = readByte()
            addrExt = readByte()
        }

        return true
    }

    override fun count(): Int = 4

    companion object {
        const val MIN_LEN = 4
    }
}